/*
 * CHttpClient.h
 *
 *  Created on: 2017年3月31日
 *      Author: chuanjiang.zh
 */

#ifndef CHTTPCLIENT_H_
#define CHTTPCLIENT_H_

#include "HttpClient.h"
#include "mongoose.h"
#include "TThread.h"


class CHttpClient : public HttpClient, public comn::Thread
{
public:
	static const int TIMEOUT = 5000;	// milliseconds

public:
	CHttpClient();
	virtual ~CHttpClient();

	virtual bool get(const std::string& url, const StringMap& headers, HttpClientCallback* callback);

	virtual bool post(const std::string& url, const StringMap& headers, const std::string& body, HttpClientCallback* callback);

	virtual bool request(const std::string& url, const std::string& httpMethod, const StringMap& headers, const std::string& body,
		HttpClientCallback* callback);

	virtual bool upload(const std::string& url, const StringMap& headers, const std::string& body, HttpClientCallback* callback);

	virtual void close();

	virtual bool isOpen();

	void handleEvent(struct mg_connection *nc, int ev, void *ev_data);

protected:
	virtual int run();

	void fireCallback(int code, const char* data, size_t length);

protected:
	struct mg_mgr mgr;
	mg_connection*	m_connection;
	HttpClientCallback*	m_callback;

	int	m_recvBytes;

};

#endif /* CHTTPCLIENT_H_ */
